//*****************************************************************************
//
// timers.c - Timers example.
//
// Copyright (c) 2005-2007 Luminary Micro, Inc.  All rights reserved.
//
// Software License Agreement
//
// Luminary Micro, Inc. (LMI) is supplying this software for use solely and
// exclusively on LMI's microcontroller products.
//
// The software is owned by LMI and/or its suppliers, and is protected under
// applicable copyright laws.  All rights are reserved.  Any use in violation
// of the foregoing restrictions may subject the user to criminal sanctions
// under applicable laws, as well as to civil liability for the breach of the
// terms and conditions of this license..
//
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
// CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
//
// This is part of revision 1716 of the Stellaris Peripheral Driver Library.
//
//*****************************************************************************

#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_types.h"
#include "debug.h"
#include "interrupt.h"
#include "sysctl.h"
#include "timer.h"
#include "adc.h"
#include "gpio.h"
#include "uart.h"
#include "osram96x16x1.h"
#include <stdlib.h>

//*****************************************************************************
//
//! \addtogroup ek_lm3s811_list
//! <h1>Timer (timers)</h1>
//!
//! This example application demonstrates the use of the timers to generate
//! periodic interrupts.  One timer is set up to interrupt once per second and
//! the other to interrupt twice per second; each interrupt handler will toggle
//! its own indicator on the display.
//
//*****************************************************************************

//*****************************************************************************
//
// Flags that contain the current value of the interrupt indicator as displayed
// on the OLED display.
//
//*****************************************************************************
unsigned long g_ulFlags;
unsigned long value, Flchange;
int intStatus=0, strAv=0;
int per=1, i=0;
unsigned long ticks=0;
char line1[] = "UART echo: UART0";
char line2[] = "                ";
int points[3];
//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif


void IntToChar(char* text, unsigned long value)
{
    char num[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    per = (value*100)/1023;
    text[0] = num[per/10];
    text[1]    = '\0';
}


void ADCIntHandler(void)
{
    
     unsigned long ulData=0;
    //
    // Clear the ADC interrupt.
    //
    ADCIntClear(ADC_BASE, 0);
    //
    // Read the data from the ADC.
    //
    //ADCSequenceDataGet(ADC_BASE, 0, &ulData);
 //      while(!ADCIntStatus(ADC_BASE, 0, false))
//            {
//           
//            }
    ADCSequenceDataGet(ADC_BASE, 0, &ulData);
    if(ulData>value+30 || ulData<value-30)
    {
       value=ulData;
       Flchange=1;
    }
    //ADCProcessorTrigger(ADC_BASE, 0);
}


void
UARTSend(const unsigned char *pucBuffer, unsigned long ulCount)
{
    //
    // Loop while there are more characters to send.
    //
    while(ulCount--)
    {
        //
        // Write the next character to the UART.
        //
        UARTCharPutNonBlocking(UART0_BASE, *pucBuffer++);
    }
}

//*****************************************************************************
//
// This example demonstrates how to setup an analog comparator and trigger
// interrupts on output changes.
//
//*****************************************************************************
int
main(void)
{
    unsigned long base;
    char text[8]; 
	char temp[]="P1=0\r";
    int  b=0, status=0;
    char choise[]="P1";
    //
    // Set the clocking to run directly from the crystal.
    //
    SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_6MHZ);

    //
    // Initialize the OLED display and write status.
    //
    OSRAM96x16x1Init(false);
    OSRAM96x16x1Clear();

    //
    // Enable the peripherals used by this example.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER2);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    //
    // Enable processor interrupts.
    //
    IntMasterEnable();

    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4);
    GPIOPinTypeGPIOOutput(GPIO_PORTC_BASE, GPIO_PIN_5);
    //
    // Configure the two 32-bit periodic timers.
    //

    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

    //UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);
    UARTSend((unsigned char *)"Parameters:\n\r", 13);

    TimerConfigure(TIMER2_BASE, TIMER_CFG_32_BIT_PER);
    base = SysCtlClockGet()/200;

    TimerLoadSet(TIMER2_BASE, TIMER_A, base);
    TimerControlStall(TIMER2_BASE, TIMER_A, true);
    TimerControlTrigger(TIMER2_BASE, TIMER_A, true);


    ADCSequenceConfigure(ADC_BASE, 0, ADC_TRIGGER_TIMER, 0);
    ADCSequenceStepConfigure(ADC_BASE, 0, 0, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END);
    ADCSequenceEnable(ADC_BASE, 0);
    ADCIntEnable(ADC_BASE, 0);
    //

    TimerEnable(TIMER2_BASE, TIMER_A);
    IntEnable(INT_UART0);
    IntEnable(INT_ADC0);


    while(1)
    {

        if(!GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4))
        {
			
            if(b==0 && status==0)
            {	
				b=1;
                status=1;
                choise[1]='1';
            }
       
        else if(b==0 && status==1)
            {
                b=1;
				status=2;
                choise[1]='2';
                   
            }
        else if(b==0 && status==2)
            {
                b=1;
				status=3;
                choise[1]='3';
                   
            }
        else if(b==0 && status==3)
            {
                b=1;
				status=0;
                  
            }     
        }
        else
        {
            if(b==1)
            {
                b=0;
                intStatus ^= 1;
                UARTSend((unsigned char *)"\n\r", 3);    
            }
        }
       
//        ADCProcessorTrigger(ADC_BASE, 0);
       
        if(Flchange==1)
        {
          IntToChar(text,  value);
          IntMasterDisable();
          OSRAM96x16x1StringDraw(text, 7, 1);
		  temp[1]=choise[1];
		  temp[3]=text[0];
          IntMasterEnable();
		  UARTSend((unsigned char *)temp,sizeof(temp));
          Flchange=0;
        }
       
   
        IntMasterDisable();
        OSRAM96x16x1StringDraw(choise, 7, 0);
        IntMasterEnable();       
   
 
       }
}
